package cn.blossoming.service.dto;

import cn.blossoming.dao.domain.VideoCommentEntity;
import cn.blossoming.dao.domain.VideoEntity;
import cn.blossoming.dao.domain.VideoTagEntity;
import cn.blossoming.service.base.consts.SessionConst;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor

public class Video {


    private Integer id;
    private String picture;
    private String title;
    private String url;
    private String sourceUrl;
    private LocalTime time;
    private Integer readNum;
    private Integer commentNum;
    private Integer praiseNum;
    private String remark;
    private boolean isPraise = false;
    private boolean isMyself = false;

    public Video(VideoEntity entity) {
        this.id = entity.getId();
        this.url = entity.getUrl();
        this.sourceUrl = entity.getSourceUrl();
        this.picture = entity.getPicture();
        this.title = entity.getTitle();
        this.time = entity.getTime();
        this.readNum = entity.getReadNum();
        this.commentNum = entity.getCommentNum();
        this.praiseNum = entity.getPraiseNum();
        this.remark = entity.getRemark();
    }

    private User user;


    private List<VideoComment> videoComments;


    private List<VideoPraise> videoPraises;


    private List<VideoRead> videoReads;


    private List<String> videoTags;


    public void setVideoTags(List<VideoTagEntity> entities) {
        this.videoTags = entities.stream().map(entity -> entity.getTag().getName()).collect(Collectors.toList());
    }

    public static List<VideoComment> videoEntityComments2VideoComments(List<VideoCommentEntity> entities) {
        Integer userId = SessionConst.getCurrentUserId();
        return entities.stream()
                .map(entity -> {
                    VideoComment comment = new VideoComment(entity);
                    comment.setCommentNum(entity.getCommentNum());
                    comment.setUser(User.builder().id(entity.getUser().getId()).name(entity.getUser().getName()).build());
                    comment.setMyself(comment.getUser().getId().equals(userId));
                    return comment;
                }).collect(Collectors.toList());
    }
}
